package com.jzx.provider;

import com.jzx.provider.ucenter.UcenterService;
import com.jzx.provider.ucenter.client.UserInfoArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * @author daihuihui
 */
@Configuration
public class WebAppConfig extends WebMvcConfigurationSupport {

    @Autowired
    private UcenterService userService;

    @Bean
    public LoginInterceptor getLoginInterceptor() {
        return new LoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //跳过拦截器的路径
        String excludePaths = "/**/login,/**/sendCode/**,/**/register,/**/findTopManager";
        String[] excludePathArray = excludePaths.split(",");
        registry.addInterceptor(getLoginInterceptor())
                .excludePathPatterns(excludePathArray)
                .addPathPatterns("/**");
        super.addInterceptors(registry);
    }

    /**
     * 添加自定义的参数解析器
     * @param argumentResolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new UserInfoArgumentResolver(userService));
    }
}